package cn.com.alibaba.utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {
	// SessionFactory对象 
			//保证只创建一次.=> 很耗费资源,线程安全的。
	
	private static SessionFactory sf;
	
	static{
		
		Configuration conf = new Configuration().configure();
		
		sf = conf.buildSessionFactory();
		
	}
	
	//获得session
	public static Session openSession(){
		
		return sf.openSession();
	}
	//获得session
	public static Session getCurrentSession(){
		
		return sf.getCurrentSession();
	}
	
	public static void close(){
		
		sf.close();
	}

	public static void main(String[] args) {
		System.out.println(HibernateUtils.openSession());
		
		System.out.println(HibernateUtils.getCurrentSession());
		
		HibernateUtils.close();//SessionFactory关闭
	}
}
